﻿using MediatR;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.Dtos;
using ProductManagement.Domain.ProductsDomain;
using ProductManagement.Infrstructre.Interface;

namespace ProductManagement.Applcation.CommandHanlers
{
    /// <summary>
    /// 产品类型反填 
    /// </summary>
    public class ProductsTypeAssignmentCommandHandlers : IRequestHandler<ProductsTypeAssignmentCommand, ProductsTypeAssignmentDto>
    {
        protected readonly InterfaceProductsTypeRepository repository;

        public ProductsTypeAssignmentCommandHandlers(InterfaceProductsTypeRepository repository)
        {
            this.repository = repository;
        }

        public async Task<ProductsTypeAssignmentDto> Handle(ProductsTypeAssignmentCommand request, CancellationToken cancellationToken)
        {
            var list = (await repository.GetAll()).First(x => x.ProductTypeId == request.id);
            if (list.PFatherId == 0)
            {
                ProductsTypeAssignmentDto newlist = new ProductsTypeAssignmentDto();
                newlist.ProductTypeId = list.ProductTypeId;
                newlist.AddName = list.AddName;
                newlist.AddTime = list.AddTime;
                newlist.PFatherId = list.PFatherId;
                newlist.DelName = list.DelName;
                newlist.DelTime = list.DelTime;
                newlist.PFatherName = "顶级产品分类";
                newlist.PTClass = list.PTClass;
                return newlist;
            }
            else
            {
                ProductsTypeAssignmentDto newlist = new ProductsTypeAssignmentDto();
                newlist.ProductTypeId = list.ProductTypeId;
                newlist.AddName = list.AddName;
                newlist.AddTime = list.AddTime;
                newlist.PFatherId = list.PFatherId;
                newlist.DelName = list.DelName;
                newlist.DelTime = list.DelTime;
                newlist.PFatherName = (await repository.GetAll()).First(x => x.ProductTypeId == list.PFatherId).PTClass;
                newlist.PTClass = list.PTClass;
                return newlist;
            }

        }
    }
}
